package neusoft.yuejian_11.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.GetInfo;
import neusoft.yuejian_11.bean.IsMatchn;
import neusoft.yuejian_11.bean.MatchBean;
import neusoft.yuejian_11.bean.MatchnBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.MatchModel;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

public class MatchnActivity extends BaseActivity implements RetrofitListener{
    protected Context context;
    private int matchId;
    private ImageButton imageButtonBack;
    private TextView textMatchName;
    private TextView textMatchTime;
    private TextView textMatchPrice;
    private TextView textUserName;
    private TextView textUserPhone;
    private Button buttonMatchn;
    private ImageButton imageButtonFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_matchn);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        textMatchName = (TextView) findViewById(R.id.text_match_name);
        textMatchTime = (TextView) findViewById(R.id.text_match_time);
        textMatchPrice = (TextView) findViewById(R.id.text_match_price);
        textUserName = (TextView) findViewById(R.id.text_user_name);
        textUserPhone = (TextView) findViewById(R.id.text_user_phone);
        buttonMatchn = (Button) findViewById(R.id.button_matchn);

        imageButtonFile = (ImageButton) findViewById(R.id.imageButtom_file);
    }

    private void initEvents() {
        imageButtonFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MatchnActivity.this, InformationMActivity.class);
                intent.putExtra("m_id",matchId);
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        buttonMatchn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MatchModel matchModel = new MatchModel();
                matchModel.matchnn(getU_id(),matchId,MatchnActivity.this);
            }
        });

    }

    private void showData() {
        matchId = getIntent().getIntExtra("m_id",-1);
    }

    private void initData() {
        MatchModel matchModel = new MatchModel();
        matchModel.matchDetail(String.valueOf(matchId),this);

        MatchModel matchModel1=new MatchModel();
        matchModel1.IsAddMatchn(getU_id(),String.valueOf(matchId),this);

        UserModel userModel = new UserModel();
        userModel.getUserInfo(getU_id(),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==1) {
            MatchBean matchBean = (MatchBean) o;
            textMatchName.setText(matchBean.getM_name());
            textMatchTime.setText(matchBean.getM_time());
            textMatchPrice.setText(String.valueOf(matchBean.getM_price()));
        }
        if (flag==2){
            GetInfo getInfo = (GetInfo) o;
            textUserName.setText(getInfo.getU_name());
            textUserPhone.setText(getInfo.getU_phone());
        }
        if (flag==5) {
            IsMatchn isMatchn = (IsMatchn) o;
            if (isMatchn.getMn_state().equals("已报名")) {
                Toast.makeText(MatchnActivity.this, "该比赛已报名", Toast.LENGTH_SHORT).show();
            }
        }
        if (flag==4){
            MatchnBean matchnBean = (MatchnBean) o;
            if (matchnBean.getSuccess().equals("1")) {

                    Toast.makeText(MatchnActivity.this, "报名成功", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(MatchnActivity.this, "报名失败", Toast.LENGTH_SHORT).show();

                }
        }
    }

    @Override
    public void onFail() {

    }
}
